/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;

import com.cea.papyrus.core.actions.AdaptDimension;
import com.cea.papyrus.core.commands.GraphNodeSetConstraintCommand;
import com.cea.papyrus.core.figures.IAbstractElementFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * this policy is use to apply resize of element
 */
public class DimensionAdaptationEditPolicy extends AbstractEditPolicy{

	public static final String DIMENSION_ADAPTATION  = "DIMENSION_ADAPTATION_POLICY";
	protected GraphNodeWithSemanticEditPart getGraphNodeEditPart() {
		return (GraphNodeWithSemanticEditPart)getHost();
	}
	protected GraphNode getGraphNode() {
		return (GraphNode)getHost().getModel();
	}


	public Command getCommand(Request request) {
		if(AdaptDimension.ADAPT_DIMENSION_ACTION_REQUEST.equals(request.getType())){
			return getAdaptDimensionCommand(); 
		}
		return super.getCommand(request);
	}
	/**
	 * this methid return a command to adapt the size of the figure 
	 * @return
	 */
	protected Command getAdaptDimensionCommand(){
		IFigure fig=this.getGraphNodeEditPart().getFigure();
		Dimension dim= getGraphNode().getSize().getCopy();
		if (fig instanceof IAbstractElementFigure){
			dim=((IAbstractElementFigure)fig).getMinimumDimension();
		}
		Rectangle newBounds= new Rectangle(getGraphNode().getPosition().getCopy(),
				dim);
		ChangeBoundsRequest request= new ChangeBoundsRequest (RequestConstants.REQ_RESIZE);
		Command c =new GraphNodeSetConstraintCommand(getGraphNode(), request,  newBounds);
		return c;
	}


}
